import axios, {type AxiosResponse, type InternalAxiosRequestConfig} from "axios"

const service = axios.create({
    baseURL: import.meta.env.VITE_APP_BASE_API,
    timeout: 5000
})

service.interceptors.request.use((config: InternalAxiosRequestConfig) => {
    // const accessToken = useAppStore().token;
    // 如果 Authorization 设置为 no-auth，则不携带 Token，用于登录、刷新 Token 等接口
    if (config.headers.Authorization !== "no-auth") {
        config.headers.Authorization = `Bearer ${import.meta.env.VITE_APP_API_KEY}`;
    } else {
        delete config.headers.Authorization;
    }
    return config;
}, (error) => Promise.reject(error))

service.interceptors.response.use((response: AxiosResponse) => {
    if(response.config.responseType == 'blob') {
        return response
    }
    if(response.status === 200) {
        return response
    }
    throw new Error(response.status.toString())
}, (error) => Promise.reject(error))

export default service